//
//  JsAnimationExecutorA.m
//  MpaClientIos
//
//  Created by Gikki Ares on 2023/4/21.
//  Copyright © 2023 Yach. All rights reserved.
//

#import "JsAnimationExecutorA.h"

@interface JsAnimationExecutorA() {
	//旋转的角度.
	float mf_angle;
	float mf_rotateSpeed;
	
	bool mb_isRotating;
	
	NSMutableDictionary * mDic_relation;
}

@end

@implementation JsAnimationExecutorA

#define sharedInstanceImpl(ClassName) {\
static ClassName * _sharedInstance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken,^{ \
	_sharedInstance = [ClassName new]; \
}); \
return _sharedInstance; \
}

+(instancetype)sharedInstance {
	sharedInstanceImpl(JsAnimationExecutorA)
}

- (instancetype)init {
	if(self = [super init]) {
		mDic_relation = [NSMutableDictionary dictionary];
	}
	return self;
}

/*
 1,speed不能>=180,否则会反方向转动
 2,所以用179作为判定点.
 3,目前在结束点有一点卡顿的效果.
 */
- (void)rotateView:(UIView *)view atSpeed:(float)speed {
	mf_rotateSpeed = speed;
	if(!mb_isRotating) {
		mb_isRotating = YES;
		[self stepRotateView:view];
	}
}

- (void)stopRotate:(UIView *)view {
	mb_isRotating = NO;
	[view.layer removeAllAnimations];
}
#define JsAnimationExecutorA_Max_Degree 160
- (void)stepRotateView:(UIView *)view {
	float anglePerSecond = 0;
	float time = 0;
		//换算为
	if(mf_rotateSpeed<JsAnimationExecutorA_Max_Degree) {
			//1秒钟转 mf_angle;
		anglePerSecond = mf_rotateSpeed;
		time = 1;
	}
	else {
			//大于180.
		anglePerSecond = JsAnimationExecutorA_Max_Degree;
		time = JsAnimationExecutorA_Max_Degree/mf_rotateSpeed;
	}
	mf_angle += anglePerSecond;
	CGAffineTransform endAngle = CGAffineTransformMakeRotation(mf_angle * (M_PI / 180.0f));
		//时间
	[UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
		view.transform = endAngle;
	} completion:^(BOOL finished) {
			//旋转速度的快慢
		if(mb_isRotating && finished) {
			[self stepRotateView:view];
		}
		else {
			//去除动画,或者进入后台,都会让finish传入no.
			mb_isRotating = NO;
		}
	}];
}

- (void)shakeView:(UIView *)view withEth:(BOOL)b {
		CGFloat t =4.0;
		CGAffineTransform translateRight  =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
		CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
		view.transform = translateLeft;
		
		[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
			[UIView setAnimationRepeatCount:2.0];
			view.transform = translateRight;
		} completion:^(BOOL finished){
			if(finished){
				[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
					view.transform =CGAffineTransformIdentity;
				} completion:NULL];
			}
	}];
}
@end
